<?php //需要根据来源来决定自己是否生产新的歌曲地址(Referer)
//echo isset($_SERVER['HTTP_REFERER']);
$callback = $_GET["callback"];
$url = $_GET["url"];//'http://box.zhangmen.baidu.com/x?op=12&count=1&title=the%20snow$$Lenka$$$$';
$songid = 0;
if(isset($_GET["songid"]))
{
	$songid = $_GET["songid"];
}
//header('Content-type: application/x-javascript');
if($callback)
{
	echo $callback . "(";
	if($url)
	{
		$ch=curl_init(); 
		$timeout=5;
		curl_setopt($ch, CURLOPT_URL, $url); 
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
		$lines_string=curl_exec($ch); 
		curl_close($ch);
		if(isset($_SERVER['HTTP_REFERER']) == 1 && $songid != 0)
		{			
			// 主机地址
			$ip = getenv('HTTP_BAE_ENV_ADDR_SQL_IP');
			// 数据库对外开放的接口
			$port = getenv('HTTP_BAE_ENV_ADDR_SQL_PORT');
			// 当前用户在BAE上的MYSQL用户名
			$username = getenv('HTTP_BAE_ENV_AK');
			// 密码
			$password = getenv('HTTP_BAE_ENV_SK');	

			$con = mysql_connect($ip.":".$port,$username,$password);
			mysql_query("SET NAMES UTF8");
			if (!$con)
			{
			  die('Could not connect: ' . mysql_error());
			}
			
			mysql_select_db("ZeXQnJerISomEZfhrZEe", $con);

			$result = mysql_query("SELECT * FROM lyrics_rank where songid = " . $songid . " order by hot");
			
			if($row = mysql_fetch_array($result))
			{			
				$prefix = "http://bcs.duapp.com/wtniaotest/hello";
				$postfix = $row["filename"];

				$lines_string = substr($lines_string, 0, strpos($lines_string, "<encode><![CDATA[") + strlen("<encode><![CDATA[")) . $prefix . substr(			$lines_string, strpos($lines_string, "]]></encode>"));
				
				$lines_string = substr($lines_string, 0, strpos($lines_string, "<decode><![CDATA[") + strlen("<encode><![CDATA[")) . $postfix . substr(			$lines_string, strpos($lines_string, "]]></decode>"));
			}
			mysql_close($con);
		}		
		$xml = new SimpleXMLElement($lines_string);//"<result><from><hi>xxx</hi></from><to>zh</to><from>us</from><to>uk</to></result>");
		echo xml2json($xml);

	}
	echo ")";
}

function xml2json($xml)
{
	$jsonStr = "";
	if(!$xml->children())
	{
		$jsonStr .= "\"" . $xml . "\"";
	}
	else
	{
		$jsonStr .= "{";
		$array = array();
		foreach($xml->children() as $child)
		{				
			if(!array_key_exists($child->getName(), $array))
			{				
				$array[$child->getName()] = array();					
			}
			array_push($array[$child->getName()], $child);
		}
		
		foreach($array as $key=>$child)
		{
			if(count($child) > 1)
			{
				$jsonStr .= "\"" .$key . "\":[";
				foreach($child as $childd)
				{
					$jsonStr .= xml2json($childd) . ",";
				}
				if($jsonStr[strlen($jsonStr) - 1] == ",")
				{
					$jsonStr = substr($jsonStr, 0, strlen($jsonStr) - 1);
				}
				$jsonStr .= "],";
			}
			else
			{
				$jsonStr .= "\"" .$key . "\":";
				$jsonStr .=  xml2json($child[0]) . ",";
			}
		}
		
		if($jsonStr[strlen($jsonStr) - 1] == ",")
		{
			$jsonStr = substr($jsonStr, 0, strlen($jsonStr) - 1);
		}
		
		$jsonStr .= "}";
	}

	if($jsonStr[strlen($jsonStr) - 1] == ",")
	{
		$jsonStr = substr($jsonStr, 0, strlen($jsonStr) - 1);
	}	
	
	return $jsonStr;
}
?>